package com.base.system.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.tree.base.TreeDataProvider;
import org.operamasks.faces.component.tree.base.TreeDataProviderAdapter;

import com.base.commons.SpringBeanHolder;
import com.base.security.entity.Resource;
import com.base.security.service.SecurityEntityManager;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_resourcesOfOneRoleBean", scope = ManagedBeanScope.SESSION)
public class ResourcesOfOneRoleBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(ResourcesOfOneRoleBean.class);
	protected SecurityEntityManager securityEntityManager = SpringBeanHolder
			.getBean("securityEntityManager");
	@Inject
	private FacesContext context;

	@SaveState
	private String roleId;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		roleId = context.getExternalContext().getRequestParameterMap().get(
				"roleId");
	}

	@Bind(id = "resourceTree")
	private TreeDataProvider resourceTreeProvider = new TreeDataProviderAdapter() {
		public Object[] getChildren(Object nodeData) {
			if (nodeData == null) {
				return new String[] { "已分配权限" };
			}
			if ("已分配权限".equals(nodeData.toString())) {
				List<String> list = new ArrayList<String>();
				if (roleId != null && !"".equals(roleId)) {
					list = securityEntityManager.getNullModule(Integer
							.valueOf(roleId));
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i) == null) {
							list.remove(i);
							list.add("其他模块");
						}
					}
				}
				if (list != null)
					return list.toArray();
			}
			if (!"已分配权限".equals(nodeData.toString())) {
				if ("其他模块".equals(nodeData.toString())) {
					List<Resource> sub = securityEntityManager
							.getResourceByModuleNullRoleId(Integer
									.valueOf(roleId));
					if (sub != null)
						return sub.toArray();
				} else {
					List<Resource> sub = securityEntityManager
							.getResourceByModuleRoleId(nodeData.toString(),
									Integer.valueOf(roleId));
					if (sub != null)
						return sub.toArray();
				}
			}
			return null;
		}

		public String getHref(Object nodeData) {
			return null;
		}

		public String getHrefTarget(Object nodeData) {
			return null;
		}

		public String getIcon(Object nodeData) {
			return null;
		}

		public String getText(Object nodeData) {
			if (nodeData instanceof Resource) {
				String str = new String();
				Resource resource = (Resource) nodeData;
				return resource.getDisplayName();
			}
			if (nodeData == null) {
				return "其他模块";
			}
			return nodeData.toString();
		}

		public boolean isCascade(Object nodeData) {
			return false;
		}

		public Boolean isChecked(Object nodeData) {
			return null;
		}

		public boolean isExpanded(Object nodeData) {
			return false;
		}
	};
}
